<!--  -->
<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="10"
          ><div class="text item">
            <el-input placeholder="请输入订单号" class="input-with-select"
            v-model="queryInfo.query" clearable @clear="getOrderList">
              <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
            </el-input></div
        ></el-col>
      </el-row>
      <!-- 订单列表区 -->
      <el-table :data="orderInfo" highlight-current-row  border size="mini">
        <el-table-column
          type="index"
          width="30">
        </el-table-column>
        <el-table-column
          property="orderId"
          label="订单编号"
          width="150">
        </el-table-column>
        <el-table-column
          label="收货地址">
          <template slot-scope="scope">
            {{scope.row.userAddressEntity.detailAddress}}
          </template>
        </el-table-column>
        <el-table-column
          label="价格" property="price"
          width="60">
        </el-table-column>
        
        <el-table-column
          label="收货人"  width="80">
          <template slot-scope="scope">
            {{scope.row.userAddressEntity.name}}
          </template>
        </el-table-column>
        <el-table-column
          label="手机号"  width="120">
          <template slot-scope="scope">
            {{scope.row.userAddressEntity.phone}}
          </template>
        </el-table-column>
        <el-table-column
          property="status"
          label="完成"  width="80">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.status==='已完成'">已完成</el-tag>
            <el-tag type="danger" v-else>未完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          property="logisticsStatus"
          label="发货"  width="80">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.logisticsStatus==='已发货'">已发货</el-tag>
            <el-tag type="danger" v-else>未发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          property="createTime"
          label="下单时间"
          width="100">
          <template slot-scope="scope">
            {{scope.row.createTime | dateFormat}}
          </template>
        </el-table-column>

        <el-table-column
          label="操作" width="210">
          <template slot-scope="scope">
            <el-button type="primary"  size="mini" @click="showEditDialog(scope.row)">修改</el-button>
            
            <el-button type="success" size="mini" @click="showOrderDetails(scope.row)">详情</el-button>
            <el-button type="danger"  size="mini" @click="deleteOrder(scope.row.orderId)">删除</el-button>
          </template>
        </el-table-column>
        
      </el-table>
        <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[5,10,15]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改订单对话框 -->
    <el-dialog title="修改订单信息" width="50%" :visible.sync ="dialogEditVisible"
    @close="editDialogClose">
      <el-form
        :model="updateOrderInfo"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm "
      >
        <el-form-item label="订单编号" >
          <el-input v-model="updateOrderInfo.orderId" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="是否完成" >
          <el-radio-group v-model="updateOrderInfo.status" >
            <el-radio label="未完成" border>未完成</el-radio>
            <el-radio label="已完成" border>已完成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否发货" >
          <el-radio-group v-model="updateOrderInfo.logisticsStatus" >
            <el-radio label="未发货" border>未发货</el-radio>
            <el-radio label="已发货" border>已发货</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataInfo">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 展示详情对话框 -->
    <el-dialog title="订单详情" :visible.sync="orderDetailsDialogVisible" width="50%">
      <el-table :data="orderDetails" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名" prop="skuName"></el-table-column>
        <el-table-column label="数量" prop="count"></el-table-column>
        <el-table-column label="价格" prop="price"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name:'Order',
  data () {
    return {
      queryInfo:{
        query:'',
        // 当前页码
        pageNum:1,
        // 当前每页显示多少条数据
        pageSize:5
      },
      dialogEditVisible:false,
      total:10,
      // 订单信息
      orderInfo:[],
      // 修改信息
      updateOrderInfo:{
      },
      // 订单详情度对话框
      orderDetailsDialogVisible: false,
      // 订单详情
      orderDetails: [{id:1}]
    };
  },
  created(){
    this.getOrderList()
  },
  methods:{
    // 监听pageSize改变
    handleSizeChange(newSize){
      // console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getOrderList()
    },
    // 监听页码值改变事件
    handleCurrentChange(newPage){
      // console.log(newPage)
      this.queryInfo.pageNum = newPage
     this.getOrderList()
    },
    // 获取订单列表
    async getOrderList(){
        const { data: res } = await this.$http.get(`/dqxmall/order/list?page=${this.queryInfo.pageNum}&limit=${this.queryInfo.pageSize}&key=${this.queryInfo.query}`);
        console.log("sousuo ",res);
      if(res.code === 0){
         this.total = res.page.totalCount
         this.orderInfo = res.page.list
      }
     
    },
    // 更改订单信息
    async  updataInfo(){
      console.log(this.updateOrderInfo)
      const{data:res} = await this.$http.post('/dqxmall/order/update',{
        id:this.updateOrderInfo.orderId,
        status:this.updateOrderInfo.status,
        logisticsStatus:this.updateOrderInfo.logisticsStatus
      })
      if(res.code !==0){ return this.$message.error("订单修改失败")}
        this.getOrderList()
        this.$message.success("订单修改成功")
        this.dialogEditVisible = false
      
    },
    // 修改对话框显示
    showEditDialog(row){
      this.updateOrderInfo = row
      this.dialogEditVisible = true
    },
    // 关闭修改对话框
    editDialogClose(){
      this.$refs.editFormRef.resetFields()
    },
    // 订单详情
    showOrderDetails(data){
      this.orderDetails = data.orderItemReturns
      this.orderDetailsDialogVisible = true
    },
    // 删除订单
    async  deleteOrder(id){
      const {data:res} = await this.$http.post('/dqxmall/order/delete',[id])
      if(res.code===0){
        this.$message.success("订单删除成功")
        this.getOrderList()
      }
      else{
        this.$message.error("订单删除失败")
      }
    }
  }

}

</script>
<style lang='less' scoped>
</style>